package com.kichina.galaxy.dev.tools.aliyun.product.utils;

import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.CreateProductRequest;
import com.aliyun.iot20180120.models.CreateProductResponse;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.kichina.galaxy.dev.tools.aliyun.AliyunConstant;
import com.kichina.galaxy.dev.tools.model.HandleBackModel;

/**
 * @author Surging
 * @version 1.0
 * @ClassName ProductCreateUtil
 * @date 2022-06-27 11:07
 * @desc 阿里云物联网设备添加工具类
 */
public class ProductCreateUtil {

    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = AliyunConstant.END_POINT;
        return new Client(config);
    }

    public static HandleBackModel createProduct(CreateProductRequest createProductRequest) {
        HandleBackModel handleBackModel = new HandleBackModel();
        Client client = null;
        try {
            client = ProductCreateUtil.createClient(AliyunConstant.ACCESS_KEY_ID, AliyunConstant.ACCESS_KEY_SECRET);
        } catch (Exception e) {
            e.printStackTrace();
            //创建阿里云连接失败，原因可能是网络异常，账号用户密码错误
            handleBackModel.setResponseTag("fail");
            handleBackModel.setResponseMsg("创建阿里云连接失败，原因可能是网络异常或者阿里云平台账号用户密码错误");
            return handleBackModel;
        }
        RuntimeOptions runtime = new RuntimeOptions();
        CreateProductResponse resp = null;
        try {
            resp = client.createProductWithOptions(createProductRequest, runtime);
        } catch (Exception e) {
            e.printStackTrace();
            //创建设备失败
            handleBackModel.setResponseTag("fail");
            handleBackModel.setResponseMsg("创建阿里云物联网平台产品失败");
            return handleBackModel;
        }
        try {
            /**
             * 创建成功返回数据实例
             * {
             *   "RequestId": "32FAD7E3-2951-5F3F-935D-418FE2488AF5",
             *   "Data": {
             *     "Description": "测试",
             *     "ProductName": "自动化添加测试设备",
             *     "AliyunCommodityCode": "iothub",
             *     "NodeType": 0,
             *     "DataFormat": 1,
             *     "ProductSecret": "sx0TpsmVg8CjuJJB",
             *     "ProductKey": "gd83f33QjE0"
             *   },
             *   "Code": "",
             *   "Success": true,
             *   "ProductKey": "gd83f33QjE0"
             * }
             */
            String jsonString = Common.toJSONString(TeaModel.buildMap(resp));
            handleBackModel.setResponseObj(jsonString);
            com.aliyun.teaconsole.Client.log(Common.toJSONString(TeaModel.buildMap(resp)));
        } catch (Exception e) {
            e.printStackTrace();
            //创建阿里云物联网平台产品成功，但返回数据解析失败
            handleBackModel.setResponseTag("success");
            handleBackModel.setResponseMsg("创建阿里云物联网平台产品成功，但返回数据解析失败");
            return handleBackModel;
        }
        handleBackModel.setResponseTag("success");
        handleBackModel.setResponseMsg("创建阿里云物联网平台产品成功");
        return handleBackModel;
    }
}
